/*

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#import "MenuTable.h"
#import <CoreFoundation/CoreFoundation.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UIImageAndTextTableCell.h>


@implementation MenuTable


-(MenuTable *)init
{
  if ((self = [super init]))
  {
      NSLog(@"Creating icon panel!");
      struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	  rect.origin.x = rect.origin.y = 0;

      iconTable = [[UITable alloc] initWithFrame:CGRectMake(0,0,rect.size.width, rect.size.height-48)];
	   UITableColumn *col = [[UITableColumn alloc] initWithTitle: @"HelloApp"
        identifier: @"hello" width: 320.0f];
      [iconTable addTableColumn: col];
      NSString *iconPath = @"/private/var/root/icons/";
	  
	  
      
      NSArray *iconsFolderContents = [[NSFileManager defaultManager] directoryContentsAtPath:iconPath];
      NSEnumerator *enumerator = [iconsFolderContents objectEnumerator];
      NSString *currentIcon;
      NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:[iconsFolderContents count]];
      imageList = [[NSMutableArray alloc] initWithCapacity:[iconsFolderContents count]];
	  while (nil != (currentIcon = [enumerator nextObject]))
	{
	  if ([[currentIcon pathExtension] isEqualToString:@"png"])
	    {
	      [tempArray addObject:currentIcon];
		  [imageList addObject:[UIImage imageAtPath:  [iconPath stringByAppendingString:currentIcon]]];
	    }
	}
      availableIcons = [[NSArray alloc] initWithArray:tempArray];
      [tempArray release];
      [iconTable setDelegate:self];
      [iconTable setDataSource:self];
      [iconTable reloadData];
	  NSLog(@"Done Creating icon panel!");
  }
  return self;
}

-(void)reloadData
{
  [iconTable reloadData];
}

-(UITable*)table
{
  return iconTable;
}
// - (void) listFiles
// {
	// NSLog(@"listFiles");

//	release previous items
	// int i;
	// for (i = 0; i < [list count]; i++) {
	  // [[list objectAtIndex:i] release];
	// }
	// [list removeAllObjects];
	// [table reloadData];

	// NSString *file;
	// NSArray* files = [[NSFileManager defaultManager] directoryContentsAtPath:path];
    // NSEnumerator *dirEnum = [files objectEnumerator];
	// while ((file = [dirEnum nextObject])) {
		// UIImageAndTextTableCell* pbCell = [[UIImageAndTextTableCell alloc] init];
		// [pbCell setTitle: file]; 
		// [list addObject: pbCell];
		// [pbCell setSelected:NO];
	// }
    // [table reloadData];
	// NSLog(@"listFiles done");
// }

#if 0
- (id) initWithTable: (UITable*)uitable
{
// TODO: dealloc
    list = [[NSMutableArray alloc] initWithCapacity:30];
	table = uitable;
	
	//path = @"/";
	//pathList = [[NSMutableArray alloc] initWithCapacity:10];

    UITableColumn *col = [[UITableColumn alloc] initWithTitle: @"XBMC Remote"
        identifier: @"xbmc" width: 320.0f];
    [table addTableColumn: col];
	
    [table setDataSource: self];
    [table setDelegate: self];

	{
		UIImageAndTextTableCell* pbCell = [[UIImageAndTextTableCell alloc] init];
		[pbCell setTitle: @"Now Playing"];
		[list addObject:pbCell];
		[pbCell setSelected:NO];
	}
	
	//[self listFiles];

	return self;
}
#endif

- (int) numberOfRowsInTable: (UITable *)table
{
    NSLog(@"numberOfRowsInTable count:%d", [availableIcons count]);
    return [availableIcons count];
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
{
    NSLog(@"cellForRow %d (%d)", row, [availableIcons count]);
    //return [availableIcons objectAtIndex:row];

    UIImageView* iconView;	
	NSString *title = [availableIcons objectAtIndex:  row];
	NSString *path = @"/private/var/root/icons/";
  //BOOL checked = NO;

  UIImageAndTextTableCell *theCell = [[UIImageAndTextTableCell alloc] init];
  [theCell setTitle:title];
  path = [path stringByAppendingString:title];
  NSLog(@"iconPath %@", path);
  iconView = [theCell iconImageView];
  [iconView setImage: [imageList objectAtIndex: row]];
  [iconView setFrame:CGRectMake(0.0f, 0.0f, 29.0f, 29.0f)];
  //[musicCellM setIcon: [[UIImage imageAtPath:@"/System/Library/CoreServices/SpringBoard.app/Default_5_Bars.png"] autorelease]];
  //[musicCellM setIcon: [[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"Settings-Sound" ofType:@"png"]] autorelease]];
  //[theCell setChecked:NO];
  return [theCell autorelease];
}

- (void)tableSelectionDidChange:(id)fp8;        // IMP=0x3241e474
{
    // int i;
    // UIImageAndTextTableCell* cell = [list objectAtIndex:[table selectedRow]];
    // [cell setSelected:NO];
  
    // NSLog(@"tableSelectionDidChange: %@", [cell title]);
    // NSLog(@"current path: %@", path);
	// [pathList insertObject:path atIndex:0];
	// NSLog(@"Path List:");
	// for (i=0; i< [pathList count]; ++i)
	// {
	   // NSLog(@"%d  %@",i, [pathList objectAtIndex:i]); 
	// }
    // path = [[NSString stringWithString:[path stringByAppendingPathComponent:[cell title]]] retain];
    // [self listFiles];
    // NSLog(@"done: %@", path);
}

- (BOOL)table:(UITable *)table showDisclosureForRow:(int)row
{
   return NO;
}

- (void) back
{
   // NSLog(@"back");
   // if ([pathList count] > 0)
   // {
	  // path = [[pathList objectAtIndex:0] retain];
	  // [pathList removeObjectAtIndex:0];
	  // [self listFiles];
   // }
}

@end